package cn.kvchoi.reactor_nio.server.handlers;

import cn.kvchoi.reactor_nio.server.EventHandler;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class AcceptEventHandler implements EventHandler
{

    @Override
    public void handleEvent(SelectionKey key)
    {  
        System.out.println(Thread.currentThread().getName() + ": Accept event handler");
        try {
            ServerSocketChannel channel = (ServerSocketChannel) key.channel();
            SocketChannel socketChannel = channel.accept();
            socketChannel.configureBlocking(false);
            //对socketChannel注册读的事件
            socketChannel.register(key.selector(), SelectionKey.OP_READ);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("=============");
    }
  
} 